/**
 * Created by Qiao.Yin on 2016/9/28.
 */
"use strict";
var router = require('koa-router')();
var config = require('../../config');
var productService = require("../../service/product/service");
let commonService = require("../../service/common/service");
let _ = require('underscore');


/**
 * 获取商品详情
 */
router.get('/productDetail', function *() {
    var id = this.request.query.id;
    var product = yield productService.getDetail({id: id, token: this.session.clientToken['access_token']});
    product['uri'] = config['apiHost']['uri'];
    product['ThumbPicture'] = product['ThumbPicture'] ? product['ThumbPicture'].split(',') : '';
    genProcess(product);
    try {
        yield this.render('product/detail', product);
    } catch (e) {
        this.redirect('sysError');
        throw new Error(e);
    }
    function genProcess(item) {
        item['process'] = ((parseInt(item['JoinPersonTimes']) / parseInt(item['PersonTimes'])) * 100).toFixed(0);
    }
});

/**
 * 订单确认
 */
router.get('/listConfirm', function *() {
    var type = this.request.query['type'];
    var itemId = this.request.query['id'];
    var userToken = this.cookies.get('access_token');
    if (!userToken) {
        this.redirect('login');
    } else {
        let num;
        var product = yield productService.getDetail({id: itemId, token: this.session.clientToken['access_token']});
        if (type == '1') {
            num = 1;
        } else if (type == '2') {
            // 秒杀份数
            num = 10;
        }
        let price = product['UnitPrice'];
        try {
            yield this.render('product/confirm', {
                productId: itemId,
                num: num,
                price: price,
                type: type,
                name: `${product['ProductName']}${product['ProductSubName']}`
            });
        } catch (e) {
            this.redirect('sysError');
            throw new Error(e);
        }
    }
});


router.get('/goPayType', function *() {
    let id = this.request.query.id;
    let num = this.request.query.num;
    let type = this.request.query.type;
    let data = {};
    let token = this.cookies.get('access_token');
    if(!token){
        this.redirect('login');
    }
    data['product'] = yield productService.getDetail({id: id, token: this.session.clientToken['access_token']});
    data['userInfo'] = yield commonService.getCurrentUserInfo({
        token: token
    });
    try {

        yield this.render('product/payType', _.extend(data, {num: num, type: type}));
    } catch (e) {
        throw new Error(e);
    }
});

router.post('/submitOrder' ,function *(){
    var params = this.request.body;
    params['token'] = this.cookies.get('access_token');
    var res = yield productService.itemOrder(params);
    this.body = res;
});



/**
 * 分类列表
 */
router.get("/category", function *() {
    let data = {};
    let cates = yield productService.getCates({
        token: this.session.clientToken['access_token']
    });
    let firstCate = yield productService.getCatePagesByProductId({
        token: this.session.clientToken['access_token'],
        id: cates[0]['Id'],
        page: 1,
        pageSize: 20
    });
    for (var c in firstCate) {
        firstCate[c]['process'] = ((parseInt(firstCate[c]['JoinPersonTimes']) / parseInt(firstCate[c]['PersonTimes'])) * 100).toFixed(0);
    }
    try {
        data['uri'] = config['apiHost']['uri'];
        data['cates'] = cates;
        data['firstCate'] = firstCate;
        yield this.render('product/cate', data);
    } catch (e) {
        this.redirect('sysError');
        throw new Error(e);
    }

});

router.get('/submitOrder', function *() {
    let userToken = this.session.token;
    let userInfo = yield commonService.getCurrentUserInfo({
        token: userToken['access_token']
    });
    debugger;
});


/**
 * 异步请求获取分类列表
 */
router.post('/product/getCatePagesByProductId', function *() {
    let params = this.request.body;
    let category = yield productService.getCatePagesByProductId({
        token: this.session.clientToken['access_token'],
        page: params['page'],
        pageSize: params['pageSize'],
        id: params['id']
    });
    this.body = category;
});
module.exports = router;